﻿using NPOI.XWPF.UserModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ShareHelper
{
    /// <summary>
    /// 写出 Word 文档帮助类
    /// </summary>
    public class WordHelper
    {
        /// <summary>
        /// word 根目录
        /// </summary>
        private XWPFDocument Root { get; }

        /// <summary>
        /// Word 写出目录
        /// </summary>
        private string Path { get; }

        /// <summary>
        ///  Word 导出类
        /// </summary>
        /// <param name="path">文件目录</param>
        /// <param name="Name">文件名称</param>
        public WordHelper(string path, string Name) : this()
        {
            Name = Name.GetFileName();
            Name = System.IO.Path.GetFileNameWithoutExtension(Name);
            Path = System.IO.Path.Combine(path, $"{Name}.doc");
        }

        /// <summary>
        /// Word 导出类
        /// </summary>
        /// <param name="path">保存路径</param>
        public WordHelper(string path) : this()
        {
            Path = path;
        }

        /// <summary>
        /// 无参构造函数
        /// </summary>
        private WordHelper()
        {
            Root = new XWPFDocument();
        }

        /// <summary>
        /// 写出图片
        /// </summary>
        /// <param name="name">图片名称</param>
        /// <param name="value">图片byte</param>
        public void WriteLine(string name, byte[] value) => Root.WriteLine(name, value);

        /// <summary>
        /// 写出文本
        /// </summary>
        /// <param name="text">文本内容</param>
        public void WriteLine(string text) => Root.WriteLine(text);

        /// <summary>
        /// 保存文件
        /// </summary>
        public void Save() => Root.Save(Path);
    }

    /// <summary>
    /// XWPFDocument 扩展类
    /// </summary>
    public static class DocumenEx
    {
        /// <summary>
        /// XWPFDocument 扩展写出图片
        /// </summary>
        /// <param name="Doc">XWPFDocument 对象</param>
        /// <param name="name">图片名称</param>
        /// <param name="value">图片byte</param>
        public static void WriteLine(this XWPFDocument Doc, string name, byte[] value)
        {
            if (value == null || value.Length <= 2) return;
            using (var ms = new MemoryStream(value))
            {
                using (var img = Image.FromStream(ms))
                {
                    var type = GetMimeType(img);
                    if (type == 0) return;//未知格式图片
                    var r1 = Doc.CreateParagraph().CreateRun();
                    using (var Data = new MemoryStream(value))
                    {
                        r1.AddPicture(Data, type, name, img.Width * 9525, img.Height * 9525);
                    }
                }
            }
        }

        /// <summary>
        /// XWPFDocument 扩展写出内容 和 图片
        /// </summary>
        /// <param name="Doc">XWPFDocument 对象</param>
        /// <param name="text">写出内容</param>
        /// <param name="name">图片名称</param>
        /// <param name="value">图片byte</param>
        public static void WriteLine(this XWPFDocument Doc, string text, string name, byte[] value)
        {
            if (value == null || value.Length <= 2) return;
            using (var ms = new MemoryStream(value))
            {
                using (var img = Image.FromStream(ms))
                {
                    var type = GetMimeType(img);
                    if (type == 0) return;//未知格式图片
                    var r1 = Doc.CreateParagraph().CreateRun();
                    r1.AppendText(text);
                    using (var Data = new MemoryStream(value))
                    {
                        r1.AddPicture(Data, type, name, img.Width * 9525, img.Height * 9525);
                    }
                }
            }
        }

        /// <summary>
        /// 获取图片类型
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        private static int GetMimeType(Image image)
        {
            var ImageCodec = ImageCodecInfo.GetImageDecoders();
            foreach (var item in ImageCodec)
            {
                if (item.FormatID == image.RawFormat.Guid)
                {
                    PictureType pictureType;
                    switch (item.FormatDescription)
                    {
                        case "BMP":
                            pictureType = PictureType.BMP;
                            break;

                        case "JPEG":
                            pictureType = PictureType.JPEG;
                            break;

                        case "GIF":
                            pictureType = PictureType.GIF;
                            break;

                        case "EMF":
                            pictureType = PictureType.EMF;
                            break;

                        case "WMF":
                            pictureType = PictureType.WMF;
                            break;

                        case "TIFF":
                            pictureType = PictureType.TIFF;
                            break;

                        case "PNG":
                            pictureType = PictureType.PNG;
                            break;

                        default:
                            return 0;
                    }
                    return (int)pictureType;
                }
            }
            return 0;
        }

        /// <summary>
        ///  XWPFDocument 扩展写出内容
        /// </summary>
        /// <param name="Doc">XWPFDocument 对象</param>
        /// <param name="value">写出内容</param>
        public static void WriteLine(this XWPFDocument Doc, string value)
        {
            var r1 = Doc.CreateParagraph().CreateRun();
            r1.AppendText(value);
        }

        /// <summary>
        /// XWPFDocument 扩展保存
        /// </summary>
        /// <param name="Doc">XWPFDocument 对象</param>
        /// <param name="path">保存路径</param>
        public static void Save(this XWPFDocument Doc, string path)
        {
            using (var out1 = new FileStream(path, FileMode.Create))
                Doc.Write(out1);
        }
    }
}